package cn.controller;

import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.entity.attendance.AttSummaryEntity;
import cn.kpi.entity.sys.Configure;
import cn.kpi.entity.sys.GeneralCodeEntity;
import cn.kpi.service.sys.IConfigureService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;
import java.util.List;

/**
 * @BelongsProject: kpispis
 * @BelongsPackage: cn.controller
 * @Author: lizeze
 * @CreateTime: 2018-07-19 14:59
 * @Description: ${Description}
 */
@Controller
@RequestMapping("configure")
public class ConfigureController {


    @Autowired
    private IConfigureService configureService;

    @RequestMapping(value = "/index")
    public String GeneralCodeList(Model model, String code) {

        try {
            model.addAttribute("code", code);

        } catch (Exception ex) {

        }
        return "sys/configure";
    }

    @RequestMapping(value = "/edit")
    public String edit(Model model) {

        try {

        } catch (Exception ex) {

        }
        return "sys/configure_edit";
    }

    @LoginCheck(description = true)
    @ResponseBody
    @RequestMapping(value = "/get_list")
    public LayTable<Configure> GetList(HttpServletRequest request) {

        List<Configure> lst = null;
        LayTable<Configure> layTable = new LayTable<Configure>();
        try {


            String code = request.getParameter("code");
            String name = request.getParameter("name");


            String where = "";
            if (name != null && name.length() > 0) {
                where += " AND name like  '%" + name + "%'";
            }
            if (code != null && code.length() > 0) {
                where += " AND code like '" + code + "%'";
            }

            lst = configureService.getAll(where);


            layTable.setCode(0);

            layTable.setData(lst);

        } catch (Exception e) {
            layTable.setCode(500);
        }
        return layTable;
    }

    @ResponseBody
    @RequestMapping(value = "/save")
    public int save(HttpServletRequest request) {

        int rs = 0;
        try {


            String json = request.getParameter("json");
            List<Configure> list = JSON.parseArray(json, Configure.class);
            rs = configureService.updateConfigure(list);


        } catch (Exception e) {
            rs = 0;
        }
        return rs;
    }


}




